package com.javawork.juc.one;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangxiaolu
 * @date 2023-02-05 22:28
 */
public class CompletableFutureFasterDemo {

    /**
     * applyToEither
     * 计算速度选用
     * @param args
     */
    public static void main(String[] args) {
        CompletableFuture<String> planA = CompletableFuture.supplyAsync(() ->{
            System.out.println("A come in");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "planA";
        });
        CompletableFuture<String> planB = CompletableFuture.supplyAsync(() -> {
            System.out.println("B come in");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "planB";
        });

        CompletableFuture<String> winner = planA.applyToEither(planB, f -> {
            return f + " is winner";
        });
        System.out.println(Thread.currentThread().getName() + "\t" + winner.join());
    }
}
